/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.generator.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.generator.entity.OmsCartItem;
import com.pig4cloud.pig.generator.entity.OmsOrder;
import com.pig4cloud.pig.generator.entity.PmsProduct;
import com.pig4cloud.pig.generator.entity.SmsFlashPromotion;
import com.pig4cloud.pig.generator.service.OmsOrderService;
import com.pig4cloud.pig.generator.service.PmsProductService;
import com.pig4cloud.pig.generator.service.SmsFlashPromotionService;
import com.pig4cloud.pig.generator.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 限时购表
 *
 * @author pig code generator
 * @date 2021-07-20 15:32:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/smsflashpromotion" )
@Api(value = "smsflashpromotion", tags = "限时购表管理")
public class SmsFlashPromotionController {

    private final  SmsFlashPromotionService smsFlashPromotionService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
	private OmsOrderService omsOrderService;
    /**
     * 分页查询
     * @param page 分页对象
     * @param smsFlashPromotion 限时购表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('generator_smsflashpromotion_get')" )
    public R getSmsFlashPromotionPage(Page page, SmsFlashPromotion smsFlashPromotion) {
        return R.ok(smsFlashPromotionService.page(page, Wrappers.query(smsFlashPromotion)));
    }


	@GetMapping("/pmslist")
	public List<SmsFlashPromotion> pmslist(SmsFlashPromotion smsFlashPromotion){
		return smsFlashPromotionService.pmslist(smsFlashPromotion);
	}

	@RequestMapping("/addorder")
	public R addorder(@RequestParam("id") Long id, @RequestParam("username")String username,@RequestParam("userId")Long userId){
		System.out.println("商品ID："+id+"当前登录人："+username+"当前登录id:"+userId);
		String noStr = SnowflakeIdWorker.generateId() + "";
		SmsFlashPromotion flash = smsFlashPromotionService.getlist(id);
		flash.setId(Long.parseLong(noStr));
		System.out.println(flash+"查询出来的信息");

		OmsOrder omsOrder=new OmsOrder();
		omsOrder.setOrderSn(noStr);
		omsOrder.setPayAmount(flash.getPrice());
		omsOrder.setPayType(1);
		omsOrder.setStatus(0);
		omsOrder.setOrderType(1);
		omsOrder.setMemberId(userId);
		omsOrder.setReceiverName(username);
		/*omsOrder.setCreateTime(new Date());*/

		omsOrderService.add(omsOrder);
		return R.ok(omsOrder);
	}


	/**
     * 通过id查询限时购表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('generator_smsflashpromotion_get')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(smsFlashPromotionService.getById(id));
    }


	@Transactional
    @GetMapping("/miaosha")
	public Object miaosha(Long id){
    	redisTemplate.setEnableTransactionSupport(true);
		System.out.println(id+"iiiiiiiiii");
		Map<String,Integer> miaosha = redisTemplate.opsForHash().entries("miaosha");
		Integer integer = miaosha.get(String.valueOf(id));
		System.out.println(integer+"gggggggg");
		redisTemplate.multi();
		if(integer>0){
			redisTemplate.opsForHash().increment("miaosha",String.valueOf(id),-1);

		}
		List exec = redisTemplate.exec();
		if(exec.size()<1&&integer<1){
			exec.add(-1);
		}/*else{
			SmsFlashPromotion byId = smsFlashPromotionService.getBy(id);

		}*/
		return exec;
	}



}
